package com.iyang.jpa.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Mu_YI
 * @Date: 2019/7/21 14:53
 * @Version 1.0
 */
public class AviationRule {

    public static void main(String[] args) {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kc = kieServices.getKieClasspathContainer();
        execute(kc);
    }

    /**
     * 执行
     * @param kc
     */
    private static void execute(KieContainer kc){
        KieSession kieSession = kc.newKieSession("IsAddRuleKS");
        List<Aviation> aviations = initData();
        for(int i = 0;i < aviations.size();i++){
            Aviation aviation = aviations.get(i);
            kieSession.insert(aviation);
            kieSession.fireAllRules();
        }
        kieSession.dispose();
    }

    /**
     * 模拟数据库查询数据业务
     * @return
     */
    private static List<Aviation> initData(){
        List<Aviation> resultAviation = new ArrayList<>();
        {
            Aviation aviation =  new Aviation(new Date(),null,new Date());
            aviation.setNumber(1L);
            resultAviation.add(aviation);
        }
        {
            Aviation aviation =  new Aviation(new Date(),new Date(),new Date());
            aviation.setNumber(2L);
            resultAviation.add(aviation);
        }
        return resultAviation;
    }

}
